package struct_config

import (
    "fmt"
    "log"
    "sort"
    "strconv"
    "strings"
)

func StrToIntArray(s string) []int {
    lst := make([]int, 0)
    for _, str := range strings.Split(s, ",") {
        if str == "" {
            continue
        }

        segs := strings.Split(str, "-")

        if len(segs) == 1 {
            addr, err := strconv.ParseUint(str, 0, 16)
            if err != nil {
                log.Println("错误的整数:", str)
                continue
            }
            lst = append(lst, int(addr))
            continue
        }

        if len(segs) == 2 {
            begin, err := strconv.ParseUint(segs[0], 0, 16)
            if err != nil {
                log.Println("错误的整数: ", segs[0])
                continue
            }
            end, err := strconv.ParseUint(segs[1], 0, 16)
            if err != nil {
                log.Println("错误的整数: ", segs[1])
                continue
            }
            if begin > end {

                begin, end = end, begin
            }
            for i := begin; i <= end; i++ {
                lst = append(lst, int(i))
            }
            continue
        }

        log.Println("不支持的形式: ", str)
        continue
    }
    return lst
}

func IntArrayToStr(lst []int) string {
    if len(lst) == 0 {
        return ""
    }
    if len(lst) == 1 {
        return fmt.Sprint(lst[0])
    }

    sort.Slice(lst, func(i, j int) bool {
        return lst[i] <= lst[j]
    })

    var result strings.Builder
    begin := 0
    for i := 1; i < len(lst); i++ {

        if lst[i-1]+1 == lst[i] {
            if i != len(lst)-1 {
                continue
            }

            result.WriteString(fmt.Sprintf("%d-%d,", lst[begin], lst[i]))
            break
        }

        if begin+1 == i {
            result.WriteString(fmt.Sprintf("%d,", lst[begin]))
        } else {
            result.WriteString(fmt.Sprintf("%d-%d,", lst[begin], lst[i-1]))
        }
        begin = i

        if i == len(lst)-1 {
            result.WriteString(fmt.Sprintf("%d", lst[i]))
            break
        }
    }
    return result.String()
}

func StrToU16Array(s string) []uint16 {
    lst := make([]uint16, 0)
    for _, str := range strings.Split(s, ",") {
        if str == "" {
            continue
        }

        segs := strings.Split(str, "-")

        if len(segs) == 1 {
            addr, err := strconv.ParseUint(str, 0, 16)
            if err != nil {
                log.Println("错误的整数:", str)
                continue
            }
            lst = append(lst, uint16(addr))
            continue
        }

        if len(segs) == 2 {
            begin, err := strconv.ParseUint(segs[0], 0, 16)
            if err != nil {
                log.Println("错误的整数: ", segs[0])
                continue
            }
            end, err := strconv.ParseUint(segs[1], 0, 16)
            if err != nil {
                log.Println("错误的整数: ", segs[1])
                continue
            }
            if begin > end {

                begin, end = end, begin
            }
            for i := begin; i <= end; i++ {
                lst = append(lst, uint16(i))
            }
            continue
        }

        log.Println("不支持的形式: ", str)
        continue
    }
    return lst
}

func U16ArrayToStr(lst []uint16) string {
    if len(lst) == 0 {
        return ""
    }
    if len(lst) == 1 {
        return fmt.Sprint(lst[0])
    }

    sort.Slice(lst, func(i, j int) bool {
        return lst[i] <= lst[j]
    })

    var result strings.Builder
    begin := 0
    for i := 1; i < len(lst); i++ {

        if lst[i-1]+1 == lst[i] {
            if i != len(lst)-1 {
                continue
            }

            result.WriteString(fmt.Sprintf("0x%X-0x%X,", lst[begin], lst[i]))
            break
        }

        if begin+1 == i {
            result.WriteString(fmt.Sprintf("0x%X,", lst[begin]))
        } else {
            result.WriteString(fmt.Sprintf("0x%X-0x%X,", lst[begin], lst[i-1]))
        }
        begin = i

        if i == len(lst)-1 {
            result.WriteString(fmt.Sprintf("0x%X", lst[i]))
            break
        }
    }
    return result.String()
}
